import moment from "moment";

export const dateTimeFormat = (date) => {
    if (!date) return "";
    try {
        return moment(date).format("YYYY-MM-DD HH:mm:ss");
    } catch (error) {
        return "";
    }
};
export const dateTimeFormat_utc = (date) => {
    if (!date) return "";
    try {
        return moment(date).utc().format('YYYY-MM-DD HH:mm:ss');
    } catch (error) {
        return "";
    }
};

export const getLastDay = () => {
    var y = new Date().getFullYear();
    var m = new Date().getMonth() + 1;
    var d = "01";
    m = m < 10 ? "0" + m : m;
    var hh = "00";
    var mm = "00";
    var ss = "00";
    return `${[y, m, d].join("-")} ${hh}:${mm}:${ss}`;
};
export const getLastDay01 = () => {
    var date1 = moment().endOf('month'); 
    var y = new Date().getFullYear();
    var m = new Date().getMonth() + 1;
    var d = new Date(y, m, 0).getDate();
    m = m < 10 ? "0" + m : m;
    var hh = "23";
    var mm = "59";
    var ss = "59";
    return `${[y, m, d].join("-")} ${hh}:${mm}:${ss}`;
};
export const dateFormat = (date, format = "YYYY-MM-DD") => {
    try {
        return moment(date).format(format);
    } catch (error) {
        return "";
    }
};

export const formatDate = (date, format = "YYYY-MM-DD") => {
    try {
        return moment(date).format(format);
    } catch (error) {
        return "";
    }
};

export const timeFormat = (date) => {
    try {
        return moment(date).format("HH:mm:ss");
    } catch (error) {
        return "";
    }
};

export const compareTime = (timeA, timeB) => {
    return moment(timeB).valueOf() - moment(timeA).valueOf();
};

export const createTimeoutPromise = (data, time = 2000) => {
    return new Promise((resolve) => {
        setTimeout(() => resolve(data), time);
    });
};

// 获取最近30天
export const getNearMonthDate = () => {
    const today = new Date();
    today.setHours(23);
    today.setMinutes(59);
    today.setSeconds(59);
    const prevMonth = new Date(today);
    const halfYearAgo = new Date(today);
    const threeMonthsAgo = new Date(today);
    prevMonth.setTime(prevMonth.getTime() - 30 * 24 * 60 * 60 * 1000 + 1000);
    halfYearAgo.setTime(halfYearAgo.getTime() - 30 * 24 * 60 * 60 * 1000 * 6 + 1000);
    threeMonthsAgo.setTime(threeMonthsAgo.getTime() - 30 * 24 * 60 * 60 * 1000 * 3 + 1000);
    return {
        today,
        threeMonthsAgo,
        prevMonth,
        halfYearAgo
    };
};

// 获取当前月份到今天
export const getThisMonthDate = () => {
    const today = new Date();
    today.setHours(23);
    today.setMinutes(59);
    today.setSeconds(59);
    const prevMonth = new Date(today);
    let date = today.getDate();
    prevMonth.setTime(prevMonth.getTime() - date * 24 * 60 * 60 * 1000 + 1000);

    return {
        today,
        prevMonth,
    };
};

export const getMinAndMaxDate = (dateList, type = 'min') => {
    let output = '';
    if(!Array.isArray(dateList)) {
        return dateList;
    }
    let types = ['min', 'max'];
    if(!types.includes(type)) {
        throw Error(`type值必须为${types.join(',')}中一个`);
    }
    dateList.forEach(item => {
        if (!output) {
            output = item;
        } else {
            let flag = (moment(item) - moment(output) > 0);
            let obj = {
                max: flag ? item : output,
                min: flag ? output : item,
            }
            if (type === 'min') {
                output = obj.min
            } else {
                output = obj.max
            }
        }
    })
    return output;
};
export const getMonthFirstDayToDay = () => {
    const firstDay = moment(new Date).startOf('month').format("YYYY-MM-DD HH:mm:ss");
    const endDay = moment(new Date()).endOf('day').format("YYYY-MM-DD HH:mm:ss");
    return [
        firstDay,
        endDay,
    ]
}

export const getToday = () => {
    return [
        moment().format("YYYY-MM-DD 00:00:00"),
        moment().format("YYYY-MM-DD 23:59:59"),
    ]
}